﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using util;
using WebApplication1.domain;

namespace WebApplication1.sysAdministrator
{
    public partial class DormitoryAdd : System.Web.UI.Page
    {
        List<Hostel> hostels = new List<Hostel>();
        List<String> list = new List<String>();
        protected void Page_Load(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            if (!Page.IsPostBack)
            {
                this.setDownList();
                this.getList();
            }
        }

        public DataTable getAdmin() {
            String sql = "SELECT * from user where role_id = 2";
            DataTable AdminName = MySqlHelper.GetDataTable(sql);
            return AdminName;
        }

        public void setDownList() {
            addAdmin.Items.Clear();
            DataTable dataTable = getAdmin(); 
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                addAdmin.Items.Add(new ListItem(
                dataTable.Rows[i][1].ToString()));
                addAdmin.Items[i].Value = dataTable.Rows[i][0].ToString();
            }
            addAdmin.Items.Insert(0, new ListItem("请选择宿管", "绑定数据"));
        }

        private void bind(DataTable Adt){
            int row = Adt.Rows.Count;
            for (int r = 0; r < row; r++){
                Hostel hostel = new Hostel((int)Adt.Rows[r][0],Adt.Rows[r][1].ToString(), Adt.Rows[r][2].ToString(), Adt.Rows[r][4].ToString());
                hostels.Add(hostel);
            } 
            GridView1.DataSource = hostels;
            GridView1.DataBind();

        }
        private void getList(){
            String sql = "SELECT * FROM `hostel` h LEFT JOIN user u on h.manager_id = u.id"; 
            DataTable Adt = MySqlHelper.GetDataTable(sql);
            bind(Adt);
        }

        protected void s_Click(object sender, EventArgs e){
            string name = selectName.Text;
            String sql = "SELECT * FROM `hostel` h LEFT JOIN user u on h.manager_id = u.id where h.name = '"+ name + "'";
            DataTable Adt = MySqlHelper.GetDataTable(sql);
            //2.获取第一行数据
            bind(Adt);
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // DataRow row = MySqlHelper.GetDataRow(sql);
            int index = e.RowIndex;
            GridViewRow row = GridView1.Rows[index];
            string id = ((Label)row.FindControl("id")).Text;
            String sql = "delete from hostel where id = " + id;
            int result = MySqlHelper.delete(sql);
            if (result == 1)
            {
                Response.Write("<script>alert('删除成功！')</script>");
                getList();
            }
            else
            {
                Response.Write("<script>alert('删除失败！')</script>");
            }
        }
        //让当前行处于修改状态
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;    
            getList();
        }
        //取消编辑
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            getList();
        }
        //更新至数据库
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int index = e.RowIndex;
            GridViewRow row = GridView1.Rows[index];
            String id = ((Label)row.FindControl("id")).Text;
            String dhostel = ((TextBox)row.FindControl("dhostel")).Text;
            String drpAdmin = ((DropDownList)row.FindControl("drpAdmin")).SelectedValue;
            String sql2 = "update hostel set name  = '" + dhostel + "', manager_id = '" + drpAdmin + "' where id = " + id;
            Response.Write("<script>alert('"+ id+dhostel+drpAdmin + "');</script>");
            MySqlHelper.GetDataTable(sql2);
            Response.Write("<script>alert('更新成功');</script>");
            GridView1.EditIndex = -1;
            getList();
            //2.获取第一行数据
           
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string admin = "";
           
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label id = e.Row.FindControl("id") as Label;
                if (id != null)
                {
                    String sql = "SELECT * FROM `hostel` h LEFT JOIN user u on h.manager_id = u.id where h.id = "+id.Text; 
                    DataTable data = MySqlHelper.GetDataTable(sql);
                    //2.获取第一行数据
                    if (data.Rows.Count > 0) {
                        admin = data.Rows[0][4].ToString();
                    }
                }
            }
            //普通项处于编辑状态,交替项的编辑状态
            if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate))
            {
                DropDownList drpAdmin = e.Row.FindControl("drpAdmin") as DropDownList;
                drpAdmin.Items.Clear();
                DataTable dataTable = getAdmin();
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    drpAdmin.Items.Add(new ListItem(
                    dataTable.Rows[i][1].ToString()));
                    drpAdmin.Items[i].Value = dataTable.Rows[i][0].ToString();
                }
                
                if (admin == "") {
                    drpAdmin.Items.Insert(0, new ListItem("请选择管理员", "绑定数据"));
                }
                if (drpAdmin != null)
                    drpAdmin.SelectedValue = admin;//设置性别下拉列表的选中状态是当前学员的性别
            }
        }

        protected void add_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
        }

        protected void okAdd_Click(object sender, EventArgs e)
        {
            string name = addName.Text;
            string admin = addAdmin.SelectedValue;
            String sql2 = "insert into hostel (name,manager_id) values ('" + name+ "','" + admin+"')";
            MySqlHelper.GetDataTable(sql2);
            Response.Write("<script>alert('添加成功');</script>");
            getList();
            Panel1.Visible = false;
           
        }

        protected void escAdd_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
        }

        protected void selectAll_Click(object sender, EventArgs e)
        {
            getList();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.getList();
        }
    }
}